//
//  HZJInfoViewController.swift
//  HZJGTEST
//
//  Created by 运车管家 on 2017/8/26.
//  Copyright © 2017年 运车管家. All rights reserved.
//

import UIKit

class HZJInfoViewController: HZJBaseViewController,UIScrollViewDelegate,UITextFieldDelegate {

    var attributeArray:[String] = []//属性合集
    var textFieldArray:[UITextField] = [] //输入框合集
    var backScrollV:UIScrollView?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        getTheAllAttribute()
        setUpUI()
    }

    
    /// 从模型中获取其所有的属性
    func getTheAllAttribute() {
        var count:UInt32  = 0
         let list = class_copyPropertyList(HZJModel.self, &count)
        
        for i in 0..<count {
            let name = property_getName(list![Int(i)])
            attributeArray.append(String(cString: name))
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func setUpUI() {
        backScrollV = UIScrollView(frame: CGRect(x: 0, y: myNavigationBar.frame.maxY, width: ScreenW, height: ScreenH - myNavigationBar.frame.maxY))
        backScrollV!.delegate = self
        backScrollV!.showsVerticalScrollIndicator = false
        backScrollV!.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickTheScrollView)))
        backScrollV!.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(clickTheScrollView)))
        view.addSubview(backScrollV!)
        
        
        var theMaxY:CGFloat = 0 //记录最后控件的最大y值，用以设置backScrollV的滑动范围
        
        //从1开始，因为第0个属性是type（类型）
        let firstY:CGFloat = 10 //第一个控件的y值
        let margin:CGFloat = 15 //上下控件之间的间隙
        let height:CGFloat = 40
        for i in 1..<attributeArray.count {
            let lab = UILabel()
            lab.tag = i
            lab.frame = CGRect(x: 15, y: firstY + CGFloat(i - 1) * (height + margin), width:ScreenW * 0.5 - 15, height: height)
            lab.text = attributeArray[i] + "："
            lab.textAlignment = .center
            lab.textColor = .random
            lab.font = UIFont.systemFont(ofSize: 20)
            backScrollV!.addSubview(lab)
            
            let textF = UITextField()
            textF.tag = lab.tag
            textF.delegate = self
            textF.layer.borderWidth = 0.5
            textF.keyboardType = .numberPad // 数字键盘
            textF.layer.borderColor = lab.textColor.cgColor
            textF.frame = CGRect(x: ScreenW * 0.5, y: lab.frame.minY, width: ScreenW * 0.5 - 30, height: lab.frame.height)
            textFieldArray.append(textF)
            backScrollV!.addSubview(textF)
            if i == attributeArray.count - 1 {
                theMaxY = textF.frame.maxY
            }
        }
        
        let saveBtn = UIButton(frame: CGRect(x: (ScreenW - 100) * 0.5, y: theMaxY + margin * 2, width: 100, height: 40))
        saveBtn.backgroundColor = .random
        saveBtn.setTitle("保存", for: .normal)
        saveBtn.addTarget(self, action: #selector(clickTheSaveBtn(btn:)), for: .touchUpInside)
        backScrollV!.addSubview(saveBtn)
        
        theMaxY = saveBtn.frame.maxY
        
        backScrollV!.contentSize = CGSize(width: ScreenW, height: theMaxY + margin * 2)
    }
    
    //mark: - UITextFieldDelegate
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        //限制输入的是数字
        if (textField.text! as NSString).length >= 3 {
            return false
        }
        let cs = NSCharacterSet(charactersIn: "0123456789").inverted
        let filtered = string.components(separatedBy: cs).joined(separator: "")
        let basicTest = string == filtered
        print(basicTest)
        return basicTest
    }
    @objc func clickTheScrollView(){
        for textF in textFieldArray {
            textF.resignFirstResponder()
        }
    }
    
    @objc func clickTheSaveBtn(btn:UIButton){
        for textF in textFieldArray {
            textF.resignFirstResponder()
        }
    }
    deinit {
        print(#function)
    }
}
